
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <stddef.h>
#include "SDL.h"
#include "SDL_image.h"

#include "../include/game.h"
#include "../include/misc.h"
#include "../include/monster.h"
#include "../include/player.h"
#include "../include/map.h"

struct s_monster {
	int x, y;
	SDL_Surface * directed_img[4];
	enum e_way_monster current_way_monster;

	//Uint32 time_;
};

t_monster monster_init() {
	t_monster monster = malloc(sizeof(*monster));
	if (!monster)
		error("Memory error");

	monster->directed_img[WEST_MONSTER] = load_image(IMG_MONSTER_LEFT);
	monster->directed_img[EAST_MONSTER] = load_image(IMG_MONSTER_RIGHT);
	monster->directed_img[NORTH_MONSTER] = load_image(IMG_MONSTER_UP);
	monster->directed_img[SOUTH_MONSTER] = load_image(IMG_MONSTER_DOWN);

	monster->current_way_monster = SOUTH_MONSTER;

	return monster;
}

void monster_free(t_monster monster) {
	assert(monster);
	int i;
	for (i = 0; i < 4; i++)
		SDL_FreeSurface(monster->directed_img[i]);

	free(monster);
}

int monster_get_x(t_monster monster) {
	assert(monster != NULL);
	return monster->x;
}

int monster_get_y(t_monster monster) {
	assert(monster != NULL);
	return monster->y;
}

void monster_set_current_way(t_monster monster, enum e_way_monster way_monster) {
	assert(monster);
	monster->current_way_monster = way_monster;
}


void monster_from_map(t_monster monster, t_map map) {
	int i, j;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_MONSTER) {
				monster->x = i;
				monster->y = j;
			}
		}
	}
}

static int monster_move_aux(t_monster monster, t_map map, int x1, int y1, int x2,
		int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	case CELL_SCENERY:
		return 0;// I changed 1 to 0 in order the bomber don't pass trough stones
		break;

	case CELL_CASE:
		return 0;//changes: idem to previous
		break;

	case CELL_BONUS:
		break;

	case CELL_GOAL:
		break;

	case CELL_MONSTER:
		break;

	case CELL_PLAYER:
		break;

	default:
		break;
	}

	// monster has moved
	return 1;
}


int monster_move(t_monster monster, t_map map) {
	int x = monster->x;
	int y = monster->y;
	int move = 0;
	//aleatoire_way_monster();
	switch (monster->current_way_monster) {
	case NORTH_MONSTER:
		if (monster_move_aux(monster, map, x, y - 1, x, y - 2) && y> 0) {
			monster->y--;
			move = 1;
		}
		break;

	case SOUTH_MONSTER:
		if (monster_move_aux(monster, map, x, y + 1, x, y + 2) && y < map_get_height(map) - 1) {
			monster->y++;
			move = 1;
		}
		break;

	case WEST_MONSTER:
		if (monster_move_aux(monster, map, x - 1, y, x - 2, y) && x > 0) {
			monster->x--;
			move = 1;
		}
		break;

	case EAST_MONSTER:
		if (monster_move_aux(monster, map, x + 1, y, x + 2, y) && x < map_get_width(map) - 1 ) {
			monster->x++;
			move = 1;
		}
		break;
	}

	if (move) {
		map_set_cell_type(map, x, y, CELL_EMPTY);
		map_set_cell_type(map, monster->x, monster->y, CELL_MONSTER);
	}
	return move;
}

//Generate aleatoire position of monster
int aleatoire_way_monster(void){

	int aleatoire_moving = 0;
	srand(time(NULL)); // initialisation of rand
	aleatoire_moving = rand()%4;

	return aleatoire_moving;
}

void monster_display(t_monster monster, SDL_Surface *screen) {
	assert(monster);
	assert(monster->directed_img[monster->current_way_monster]);
	assert(screen);


	SDL_Rect place;
	place.x = monster->x * SIZE_BLOC;
	place.y = monster->y * SIZE_BLOC;

	SDL_BlitSurface(monster->directed_img[monster->current_way_monster], NULL, screen,
			&place);

	switch (aleatoire_way_monster()) {
	case NORTH_MONSTER :
		monster_set_current_way(monster,NORTH_MONSTER );
		break;

	case SOUTH_MONSTER:
		monster_set_current_way(monster,SOUTH_MONSTER );
		break;
	case WEST_MONSTER :
		monster_set_current_way(monster,WEST_MONSTER );
		break;
	case EAST_MONSTER:
		monster_set_current_way(monster,EAST_MONSTER );
		break;

	}
}

